﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using Sinacor.OMM.Configuration.Router;
using Sinacor.OMM.Configuration;
using System.Threading;
using Sinacor.OMM.HealthMonitor.Collector;

namespace Sinacor.OMM.Router
{
    partial class RouterService : ServiceBase
    {
        static ProcessLogic.Router _router = null;
        static ThreadStart _startRouterHandler = new ThreadStart(Start);
        static Thread _startRouterThread = new Thread(_startRouterHandler);
        static HealthMonitorCollector _healthMonitor;
        public RouterService()
        {
            InitializeComponent();
            this.ServiceName = "BM&FBOVESPA Router Service";
        }

        private static void Start()
        {
            //while (!System.Diagnostics.Debugger.IsAttached)
            //    System.Threading.Thread.Sleep(1000);

            _router = new Sinacor.OMM.Router.ProcessLogic.Router();
            _router.Start();
        }

        protected override void OnStart(string[] args)
        {
            _startRouterThread.Start();
            _healthMonitor = HealthMonitorCollector.GetInstance();
        }

        protected override void OnStop()
        {
            if (_healthMonitor != null)
                _healthMonitor.Dispose();

            if (_router != null && _router.IsStarted)
                _router.Stop();

            _router = null;
            _startRouterHandler = null;
            _startRouterThread = null;
        }
    }
}
